﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Browser;
using CommunitySystem.Elements;
using CommunitySystem.Assert;
using CommunitySystem;
using CommunitySystem.Map;
using EHR.Common;

namespace CommunitySystem
{
    public partial class MasterPage : UserControl
    {
        public static event EventHandler Logout;

        private HomeMap _page0;//首页
        private SystemManagerControl _systemManagerControl;
        private System.Windows.Threading.DispatcherTimer timer = null;
        private SystemService.SystemServiceClient client = null;
        public MasterPage()
        {
            InitializeComponent();

            #region Timely checking
            //if (timer == null)
            //{
            //    timer = new System.Windows.Threading.DispatcherTimer();
            //    timer.Interval = new TimeSpan(0, 0, 0, 0, 100);
            //    timer.Tick += new EventHandler(timer_Tick);
            //    timer.Start();
            //}
            //this.MouseLeftButtonDown += new MouseButtonEventHandler(HomePage_MouseLeftButtonDown);
            //this.KeyDown += new KeyEventHandler(HomePage_KeyDown);
            #endregion
            //ScrollingUp.MouseEnter += new MouseEventHandler(ScrollingUp_MouseEnter);
            //ScrollingUp.MouseLeave += new MouseEventHandler(ScrollingUp_MouseLeave);
            Session.dog.PropertyChanged -= new System.ComponentModel.PropertyChangedEventHandler(dog_PropertyChanged);
            Session.dog.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(dog_PropertyChanged);
            client = new SystemService.SystemServiceClient();
            xBusy2.IsBusy = true;
            LayoutRoot.Children.Remove(headerGrid);
            siteHeader.HeaderContent = headerGrid;
            client.GetCommunitySysteLoginTimeCompleted += new EventHandler<SystemService.GetCommunitySysteLoginTimeCompletedEventArgs>(client_GetCommunitySysteLoginTimeCompleted);
            client.GetCommunitySysteLoginTimeAsync(Session.LoginUser.LoginName);
        }
        //private void ScrollingUp_MouseEnter(object sender, MouseEventArgs e)
        //{
        //    animation.Pause();//暂停

        //}

        //private void ScrollingUp_MouseLeave(object sender, MouseEventArgs e)
        //{
        //    animation.Resume();//恢复
        //}
        //void ScrollingUp_MouseLeave(object sender, MouseEventArgs e)
        //{
        //    animation.Begin();
        //}

        //void ScrollingUp_MouseEnter(object sender, MouseEventArgs e)
        //{
        //    animation.Stop();
        //}

        private DateTime _LastOperation = DateTime.Now;
        void HomePage_KeyDown(object sender, KeyEventArgs e)
        {
            _LastOperation = DateTime.Now;
        }

        void HomePage_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            _LastOperation = DateTime.Now;
        }

        void timer_Tick(object sender, EventArgs e)
        {
            var last = DateTime.Now - _LastOperation;
            if (last.Minutes > 1)
            {
                tbExit_Click(null, null);
                timer.Tick -= new EventHandler(timer_Tick);
            }
        }

        void dog_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            //if (e.PropertyName.Equals("HasDog"))
            //{
            //    PassWordDog dog = sender as PassWordDog;
            //    if (dog.HasDog == false && Session.NeedPasswordDog)
            //    {
            //        SilverlightMessageBoxLibrary.Message.ErrorMessage("密码狗拔出，将推出登录!");
            //        tbExit_MouseLeftButtonUp(null, null);
            //    }
            //}
        }

        void client_GetCommunitySysteLoginTimeCompleted(object sender, SystemService.GetCommunitySysteLoginTimeCompletedEventArgs e)
        {
            var list = e.Result;
            if (list.Count > 1)
            {
                tbtoday.Visibility = System.Windows.Visibility.Visible;
                tbTotal.Visibility = System.Windows.Visibility.Visible;
                TodayCount.Visibility = System.Windows.Visibility.Visible;
                totalCount.Visibility = System.Windows.Visibility.Visible;
                totalCount.Text = list[1] + "";
                TodayCount.Text = list[0] + "";
            }
        }


        #region  翻转方法实现
        private void ShowHomeMap(int pid, BusyIndicator _busyIndicator)
        {
           
            //    _page0 = new HomeMap(pid, _busyIndicator) { Nav = new NavMoveSwitch() };
            //    _page0.Nav.FirstPage = _page0;
           
            //flipSwitch.SetNewPage(_page0, _page0.Nav);
        }

        //系统管理
        private void ShowXTGL(int id, BusyIndicator busyIndicator)
        {
            _systemManagerControl = null;
            if (_systemManagerControl == null)
            {
                _systemManagerControl = new SystemManagerControl(id, busyIndicator) { Nav = new NavMoveSwitch() };
                _systemManagerControl.Nav.FirstPage = _systemManagerControl;
            }
            flipSwitch.SetNewPage(_systemManagerControl, _systemManagerControl.Nav);
        }
        #endregion
        /// <summary>
        /// 加载时触发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            App._FlipSwitch = flipSwitch;
            tbUserName.Text = Session.LoginUser.RealName;

            //ShowHomeMap(0, xBusy);
        }
        
        
        /// <summary>
        /// 注销
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tbExit_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            if (Logout != null)
            {
                Logout(this, EventArgs.Empty);
            }
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tbUpdatePwd_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            //ChildBox.AutoShow("修改密码", new UpdatePwdView());
        }


        #region 点击菜单触发的翻转方法
        /// <summary>
        /// 点击菜单触发的翻转方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void siteHeader_ClickTabHeader(object sender, Elements.ClickTabHeaderEventArgs e)
        {
            //xBusy.IsBusy = false;  
            Worker.IsBusy = true;
            int id = e.id;
            DataService.MenuItem mode = e.model;
            try
            {
                object obj = Activator.CreateInstance(Type.GetType(mode.Target), new object[] { mode.ID, xBusy });
                obj.GetType().GetProperty("Nav").SetValue(obj, new NavMoveSwitch() { FirstPage = obj as FrameworkElement }, null);
                flipSwitch.SetNewPage(obj as CommunitySystem.Views.IAsyncCtrl, obj.GetType().GetProperty("Nav").GetValue(obj, null) as NavMoveSwitch);
            }
            catch (Exception ex)
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("导航地址不正确！"); 
                xBusy.IsBusy = false;
            }
            Worker.IsBusy = false;
            return;
            
        }
        #endregion

        

        /// <summary>
        /// 退出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tbExit_Click(object sender, MouseButtonEventArgs e)
        {
            //HtmlWindow html = HtmlPage.Window;
            //html.Navigate(new Uri("http://oa.spsp.cn/Main.aspx"));
            if (Logout != null)
            {
                Logout(this, EventArgs.Empty);
            }
           
        }

        /// <summary>
        /// 修改密码
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UPPass_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            UpdatePasswordWindow Up = new UpdatePasswordWindow();
            Up.Show();

            Up.Closed += new EventHandler(Up_Closed);
        }

        /// <summary>
        /// 提示修改成功
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void Up_Closed(object sender, EventArgs e)
        {
            UpdatePasswordWindow DDItem = (UpdatePasswordWindow)sender;
            if (DDItem.DialogResult == true)
            {
                SilverlightMessageBoxLibrary.Message.InfoMessage("修改成功！"); 
            }

        }
    }
}
